package starComponent.experiments.expeComparison;

import java.io.IOException;

import starComponent.controls.ObserverThumb;
import starComponent.elements.A_Thumb;

public class Validator implements ObserverThumb{
	
	public static final int VALIDATE_IF_BIGGER_THAN = 0;
	public static final int VALIDATE_IF_LESS_THAN = 1;
	public static final int VALIDATE_IF_EQUALS = 2;
	
	private String branchToObserve;
	private double valueToValidate;
	private int mode;
	private TraceExperiment trace;
	
	public Validator(TraceExperiment trace, String branchToObserve, double valueToValidate, int mode){
		this.branchToObserve = branchToObserve;
		this.valueToValidate = valueToValidate;
		this.mode = mode;
		this.trace = trace;
	}
	
	@Override
	public void update(A_Thumb thumb) {
		if(thumb.getBranch().getName().equals(branchToObserve)){
			if(mode==VALIDATE_IF_BIGGER_THAN){
				if(thumb.getValue()>=valueToValidate)
					try {
						trace.end();
					} catch (IOException e) {
						e.printStackTrace();
					}
			}
			else if(mode==VALIDATE_IF_LESS_THAN){
				if(thumb.getValue()<=valueToValidate)
					try {
						trace.end();
					} catch (IOException e) {
						e.printStackTrace();
					}
			}
			else if(mode==VALIDATE_IF_EQUALS){
				if(thumb.getValue()==valueToValidate)
					try {
						trace.end();
					} catch (IOException e) {
						e.printStackTrace();
					}
			}
			else System.err.println("Validator: mode impossible");
		}
	}

	public double getValueToValidate() {
		return valueToValidate;
	}



}
